//
//  DiaryView.swift
//  ShouChong
//
//  Created by yw on 2024/8/15.
//

import SwiftUI

struct DiaryView: View {
    let db = SQLiteManager.shareManager().db
    
    @ObservedObject var vm:DiaryViewModel = DiaryViewModel()
    @State var userContent:String = ""
    @State var placeholderText:String = "请写入您今天的感受吧"
    
    var body: some View {
        ScrollView {
            
            placeHolderView
                .padding(.horizontal,10)
                .padding(.top,10)
            HStack {
                Text("您可以记录下每次运动后您的感受如何")
                    .modifier(MainTextModifier(fontSize: 13, fontWeight:.light, foreColor: Color("color_gray")))
                    .padding(.bottom,40)
                Spacer()
            }.padding(.horizontal,10)
            
            ShakeButton(
                label: Text("完 成")
                    .modifier(MainButtonModifier(width: kScreenWidth - 50))
            ) {
                saveUserDiary()
            }
            .padding(.bottom,30)
            
            Divider()
                .padding(.horizontal,8)
            diaryListView
            

        }.onTapGesture {
            UIApplication.shared.endEditing()
        }.onAppear{
            vm.getList()
        }
    }
    
    var diaryListView:some View {
        VStack {
            ForEach(vm.list) { item in
                VStack(alignment:.leading) {
                    Text(item.content)
                        .modifier(MainTextModifier(fontSize: 14, fontWeight: .light, foreColor: Color("color_text_black")))
                        .padding(.bottom,6)
                    HStack {
                        Spacer()
                        Text(item.dateString)
                            .modifier(MainTextModifier(fontSize: 13, fontWeight: .light, foreColor: Color("color_text_black")))
                    }
                }
                .contentShape(Rectangle())
                .contextMenu(menuItems: {
                    ShakeButton(label: Text("删除")) {
                        deleteRow(item: item)
                    }
                })
            }
            .padding(.horizontal,10)
            .padding(.vertical,6)
            .background(Color("color_white"))
            .cornerRadius(10)
            
        }
        .padding(.horizontal,10)
        .padding(.vertical,6)
        
        
    }
    
    var placeHolderView:some View {
        ZStack(content: {
            if userContent.isEmpty {
                TextEditor(text:$placeholderText)
                    .modifier(MainTextModifier(fontSize: 14, fontWeight: .regular, foreColor: .gray))
                    .disabled(true)
//                    .padding()
                    .frame(height: 180)
                    .cornerRadius(10)
                    .font(Font.system(size: 16))
                    
            }
            TextEditor(text: $userContent)
                .modifier(MainTextModifier(fontSize: 14, fontWeight: .regular, foreColor: .gray))
                .opacity(userContent.isEmpty ? 0.25 : 1)
//                .padding()
                .frame(height: 180)
                .cornerRadius(10)
                .font(Font.system(size: 16))
        })
    }
    
    private func saveUserDiary() {
        
        if userContent.isEmpty {
            YWToast.show(text: "请输入点内容吧")
            return
        }
        UIApplication.shared.endEditing()
        vm.addRow(content: userContent, dateString: String().date2String(Date(),dateFormat: .format_MdHm))
        userContent = ""
        vm.getList()
        
    }
    
    private func deleteRow(item:DiaryModel) {
        vm.deleteRow(model: item)
        vm.getList()
    }
}

